package com.example.pcbcsmbackend.controller;

import com.example.pcbcsmbackend.entity.Address;
import com.example.pcbcsmbackend.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/address")
@CrossOrigin(origins = "http://localhost:8080")
public class AddressController {
    
    @Autowired
    private AddressService addressService;
    
    // 获取用户地址列表
    @GetMapping("/user/{userId}")
    public ResponseEntity<Map<String, Object>> getUserAddresses(@PathVariable Integer userId) {
        Map<String, Object> response = new HashMap<>();
        try {
            List<Address> addresses = addressService.getAddressesByUserId(userId);
            response.put("success", true);
            response.put("data", addresses);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    // 添加地址
    @PostMapping("/add")
    public ResponseEntity<Map<String, Object>> addAddress(@RequestBody Map<String, Object> request) {
        Map<String, Object> response = new HashMap<>();
        try {
            Integer userId = (Integer) request.get("userId");
            String address = (String) request.get("address");
            String contactName = (String) request.get("contactName");
            String contactPhone = (String) request.get("contactPhone");
            Boolean isDefault = (Boolean) request.get("isDefault");
            
            Address newAddress = addressService.addAddress(userId, address, contactName, contactPhone, isDefault != null && isDefault);
            response.put("success", true);
            response.put("message", "地址添加成功");
            response.put("data", newAddress);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    // 更新地址
    @PutMapping("/update")
    public ResponseEntity<Map<String, Object>> updateAddress(@RequestBody Map<String, Object> request) {
        Map<String, Object> response = new HashMap<>();
        try {
            Integer addressId = (Integer) request.get("addressId");
            String address = (String) request.get("address");
            String contactName = (String) request.get("contactName");
            String contactPhone = (String) request.get("contactPhone");
            Boolean isDefault = (Boolean) request.get("isDefault");
            
            Address updatedAddress = addressService.updateAddress(addressId, address, contactName, contactPhone, isDefault != null && isDefault);
            response.put("success", true);
            response.put("message", "地址更新成功");
            response.put("data", updatedAddress);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    // 删除地址
    @DeleteMapping("/{addressId}")
    public ResponseEntity<Map<String, Object>> deleteAddress(@PathVariable Integer addressId) {
        Map<String, Object> response = new HashMap<>();
        try {
            addressService.deleteAddress(addressId);
            response.put("success", true);
            response.put("message", "地址删除成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    // 设置默认地址
    @PutMapping("/default")
    public ResponseEntity<Map<String, Object>> setDefaultAddress(@RequestBody Map<String, Object> request) {
        Map<String, Object> response = new HashMap<>();
        try {
            Integer addressId = (Integer) request.get("addressId");
            addressService.setDefaultAddress(addressId);
            response.put("success", true);
            response.put("message", "默认地址设置成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
} 